package com.financing.platform.web.api;


import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.dto.FpFaUserContractDTO;
import com.financing.platform.dto.OrderDTO;
import com.financing.platform.service.IFpFaUserContractService;
import com.financing.platform.service.IOrderService;
import com.financing.platform.util.DTOdateSet;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.financing.platform.common.Pager;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.List;

@Controller("fpFaUserContractController")
@RequestMapping("/api/fpFaUserContract")
public class FpFaUserContractController extends BaseAction  {

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFpFaUserContractService fpFaUserContractService;
	@Autowired
	IOrderService orderServiceImpl;
	
	@RequestMapping("/listFpFaUserContract")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpFaUserContractDTO fpFaUserContract, Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpFaUserContract list begin .......");
			
			fpFaUserContract.setBegin(pager.getStartIndex());
			fpFaUserContract.setPageSize(pager.getPageSize());
			
			int count = fpFaUserContractService.queryListCount(fpFaUserContract);
			
			pager.setTotalCount(count);
			
			List<FpFaUserContractDTO> fpFaUserContractList = null;
			
			if(count > 0){
				fpFaUserContractList = fpFaUserContractService.queryList(fpFaUserContract);
			}else{
				fpFaUserContractList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpFaUserContractList);
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpFaUserContract list end .......");
		}catch(Exception e){
			log.error("query fpFaUserContract error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/addFpFaUserContract")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpFaUserContractDTO fpFaUserContract){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add fpFaUserContract  begin .......");
			DTOdateSet.setUpdateInfo(fpFaUserContract, request.getSession());
			DTOdateSet.setCreateInfo(fpFaUserContract, request.getSession());
			
			int id = fpFaUserContractService.insert(fpFaUserContract);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add fpFaUserContract list end .......");
		}catch(Exception e){
			log.error("add fpFaUserContract error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailFpFaUserContract")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpFaUserContractDTO fpFaUserContract){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail fpFaUserContract  begin .......");
			
			Integer id = fpFaUserContract.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FpFaUserContractDTO dto = fpFaUserContractService.loadById(id);
			
			result.setAttribute("dto",dto);

			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail fpFaUserContract  end .......");
		}catch(Exception e){
			log.error("query detail fpFaUserContract error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteFpFaUserContract")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpFaUserContractDTO fpFaUserContract){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete fpFaUserContract  begin .......");
			
			Integer id = fpFaUserContract.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			fpFaUserContractService.delete(id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete fpFaUserContract  end .......");
		}catch(Exception e){
			log.error("delete fpFaUserContract error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateFpFaUserContract")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpFaUserContractDTO fpFaUserContract){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update fpFaUserContract  begin .......");
			
			Integer id = fpFaUserContract.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			DTOdateSet.setUpdateInfo(fpFaUserContract, request.getSession());
			
			fpFaUserContractService.update(fpFaUserContract);

			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update fpFaUserContract list end .......");
		}catch(Exception e){
			log.error("update fpFaUserContract error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceFpFaUserContract")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FpFaUserContractDTO fpFaUserContract){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce fpFaUserContract  begin .......");
			
			Integer id = fpFaUserContract.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			DTOdateSet.setUpdateInfo(fpFaUserContract, request.getSession());
			
			fpFaUserContractService.updateForce(fpFaUserContract);


			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce fpFaUserContract list end .......");
		}catch(Exception e){
			log.error("updateForce fpFaUserContract error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	/**
	 * 针对订单-点击申请合同
	 * @param request
	 * @param orderId
	 * @return
	 */
	@RequestMapping("/findFaUserContract.shtml")
	@ResponseBody
	public ResultMap findFaUserContract(HttpServletRequest request,String orderId){
		ResultMap result = new ResultMap();

		try{
			logger.info("query detail fpFaUserContract  begin .......");

			//先获取合同信息
			FpFaUserContractDTO fpFaUserContract = new FpFaUserContractDTO();
			fpFaUserContract.setOrderId(orderId);
			fpFaUserContract = fpFaUserContractService.queryListGetFirst(fpFaUserContract);

			result.setAttribute("dto",fpFaUserContract);

			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("query detail fpFaUserContract  end .......");
		}catch(Exception e){
			log.error("query detail fpFaUserContract error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return result;
	}
	/**
	 * 针对订单-点击提交申请合同
	 * @param request
	 * @param userContractDTO
	 * @return
	 */
	@RequestMapping("/saveFaUserContract.shtml")
	@ResponseBody
	public ResultMap saveFaUserContract(HttpServletRequest request,FpFaUserContractDTO userContractDTO){
		ResultMap result = new ResultMap();

		try{
			logger.info("in  saveFaUserContract  begin .......");
			OrderDTO orderDTO = new OrderDTO();
			orderDTO.setId(userContractDTO.getOrderId());
			orderDTO = orderServiceImpl.queryOrderDetail(orderDTO);
			if(orderDTO == null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			if(userContractDTO.getId() == null){

				//添加
				DTOdateSet.setUpdateInfo(userContractDTO, request.getSession());
				DTOdateSet.setCreateInfo(userContractDTO, request.getSession());
				userContractDTO.setContractStatus("02");
				userContractDTO.setUserId(orderDTO.getoFAId());
				fpFaUserContractService.insert(userContractDTO);
			}else{

				//修改
				userContractDTO.setUserId(orderDTO.getoFAId());
				DTOdateSet.setUpdateInfo(userContractDTO, request.getSession());
				fpFaUserContractService.update(userContractDTO);
			}

			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);

			logger.info("in findFaUserContract list end .......");
		}catch(Exception e){
			log.error("findFaUserContract error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}

		return result;
	}
}
